# Melina Demertzi

# This files creates a valid xml representation of the mindmap
# given as a set of lists for the nodes and edges
# the output gets written on disk

import sys
import xml.etree.ElementTree as ElementTree

class XMLmindmap:
	nodes = []
	edges = []
	write_file = ""
	mindmap_name = ""
	tree = ElementTree.ElementTree() 

	# initialize. Get the necessary information
	def __init__(self, mname, nodes_list, edges_list, fout):
		self.nodes = nodes_list
		self.edges = edges_list
		self.write_file = fout
		self.mindmap_name = mname

	def create_xmlmindmap(self):
		#create root node and add to tree
		root = ElementTree.Element("mindmap")
		self.tree._setroot(root)
		
		#create title element and append
		title_element = ElementTree.Element(self.mindmap_name)
		root.append(title_element)
        
        	#create outermost nodes element and append
        	nodes_element= ElementTree.Element("nodes")	
        	title_element.append(nodes_element)
        	
		# create the nodes from list
		for node in self.nodes:
			node_element = ElementTree.Element("node")
			name_element = ElementTree.Element("name")
			name_element.text = node
			node_element.append(name_element)
			nodes_element.append(node_element)

		#create outer most edges element and attach
		edges_element = ElementTree.Element("edges")
		title_element.append(edges_element)

		#create edges from list
		for edge in self.edges:
			edge_element = ElementTree.Element("edge")
		
			# attach from node
			from_element = ElementTree.Element("from")
			from_element.text = edge[0]
			edge_element.append(from_element)

			#attach to node
			to_element = ElementTree.Element("to")
			to_element.text = edge[1]
			edge_element.append(to_element)
			
			#attach color node
			color = ElementTree.Element("color")
			color.text = 'black'
			edge_element.append(color)
			
			edges_element.append(edge_element)


	# store mindmap in file
	def store_xmlmindmap(self):
		self.tree.write(self.write_file, 'utf-8')
